package com.example.component.d_content_provider.base

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.example.component.R

class ContactAdapter(private val contacts: List<String>) :
    RecyclerView.Adapter<ContactAdapter.ViewHolder>() {

    inner class ViewHolder(itemView: ViewGroup) : RecyclerView.ViewHolder(itemView) {
        val textView: TextView = itemView.findViewById(R.id.textView)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.contact, parent, false)
        val viewHolder = ViewHolder(view as ViewGroup)
        viewHolder.itemView.setOnClickListener {
            val position = viewHolder.adapterPosition
            val contact = contacts[position]
            Toast.makeText(parent.context, "click on item $contact", Toast.LENGTH_SHORT)
                .show()
        }

        return viewHolder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val contact = contacts[position]
        holder.textView.text = contact
    }

    override fun getItemCount(): Int {
        return contacts.size
    }
}